package com.show.business.integrate.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.integrate.bean.SmsConfig;
import com.show.business.integrate.mapper.SmsConfigMapper;
import com.show.business.integrate.service.ISmsConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.show.business.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 平台管理-短信配置表 服务实现类
 * </p>
 *
 * @author dvlp
 * @since 2024-10-15
 */
@Service
public class SmsConfigServiceImpl extends ServiceImpl<SmsConfigMapper, SmsConfig> implements ISmsConfigService {


    @Override
    public List<SmsConfig> getList(SmsConfig smsConfig) {
        return baseMapper.getList(smsConfig);
    }

    @Override
    public AjaxResult saveInfo(SmsConfig smsConfig) {
        if(StrUtil.hasBlank(smsConfig.getAccessKeyId(),smsConfig.getAccessKeySecret(),smsConfig.getTemplateCode(),smsConfig.getTemplateType(),smsConfig.getId())){
            return AjaxResult.error("必填项不能为空");
        }
        smsConfig.setDelFlag("0");
        smsConfig.setCreateTime(DateTime.now());
        smsConfig.setCreateBy(getUsername());
        baseMapper.insert(smsConfig);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult editInfo(SmsConfig smsConfig) {
        if(StrUtil.hasBlank(smsConfig.getAccessKeyId(),smsConfig.getAccessKeySecret(),smsConfig.getTemplateCode(),smsConfig.getTemplateType(),smsConfig.getId())){
            return AjaxResult.error("必填项不能为空");
        }
        smsConfig.setUpdateTime(DateTime.now());
        smsConfig.setUpdateBy(getUsername());
        baseMapper.updateById(smsConfig);
        return AjaxResult.success();
    }

    /**
     * 启用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult enable(String groupId){
        SmsConfig smsConfig=baseMapper.selectById(groupId);
        smsConfig.setStatus("1");
        baseMapper.updateById(smsConfig);
        return AjaxResult.success();
    }

    /**
     * 禁用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult disable(String groupId){
        SmsConfig smsConfig=baseMapper.selectById(groupId);
        smsConfig.setStatus("0");
        baseMapper.updateById(smsConfig);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo(String id) {
        return AjaxResult.success(baseMapper.getInfo(id));
    }

    @Override
    public AjaxResult delInfo(String id) {
        baseMapper.updateDel(id);
        return AjaxResult.success();
    }
    @Override
    public AjaxResult delInfos(String[] ids) {
        for(String id:ids){
            baseMapper.updateDel(id);
        }
        return AjaxResult.success();
    }

    @Override
    public SmsConfig getConfig() {
        return baseMapper.getOneInfo();
    }

    @Override
    public SmsConfig getInfoByType(String templateType) {
        return baseMapper.getInfoByType(templateType);
    }
}
